<%@page import="org.swim.remote.entities.ICollaboration.Visibility"%>
<%@page import="org.swim.remote.entities.ICollaboration.Status"%>
<%@page import="org.swim.remote.entities.ICollaboration"%>
<%@page import="org.swim.remote.entities.IMessage"%>
<%@page import="org.swim.remote.entities.IUser"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" import="org.swim.web.utils.*"%>
<%@page import="java.util.List"%>

<!DOCTYPE html>
<html lang="en">
<head>
<%@include file="include/header.jsp"%>
<style type="text/css">

/* 	Collaboration */
.collaboration-item {
	padding: 15px;
	padding-top: 10px;
	margin: 15px;
}

.collaboration-item .alert {
	padding-right: 14px;
}

.collaboration-item:HOVER {
	background: #EEE;
}

.collaboration-item p {
	line-height: normal;
}
</style>

<title><%=SwimConst.getTitle()%></title>
</head>
<body>
	<%@include file="include/navbar.jsp"%>
	<%@include file="include/error-handling.jsp"%>

	<div class="container">
		<div class="row">
			<div class="span8 ui-corner-all" style="background: #DDD">
				<div class="row">
					<%
						IUser me = (IUser) request.getAttribute("owner");
						List lastColls = (List) request.getAttribute("lastColls");
						ICollaboration collaboration;
						IMessage lastmessage;
						for (int i = 0; i < lastColls.size(); i++) {
							collaboration = (ICollaboration) lastColls.get(i);
							lastmessage = ((ICollaboration) lastColls.get(i))
									.getMessagesList().get(0);
					%>
					<div class="span8">
						<div class="collaboration-item medium-text ui-corner-all">
							<%
								if (collaboration.getStatus().equals(Status.COMPLETED)) {
							%>
							<div class="label label-info pull-right">Completed</div>
							<%
								} else if (collaboration.getStatus().equals(Status.CONFIRMED)) {
							%>
							<div class="label label-success pull-right">Accepted</div>
							<%
								} else if (collaboration.getStatus().equals(Status.REJECTED)) {
							%>
							<div class="label label-important pull-right">Rejected</div>
							<%
								} else if (collaboration.getStatus().equals(Status.REQUESTED)) {
							%>
							<div class="label label-warning pull-right">Requested</div>
							<%
								}
							%>
							<h4>
								<%=collaboration.getTitle()%><br />

								<%
									boolean ownerFound = false;
										if (SwimUtils.isLogged(request)) {

											if (collaboration.getIUserBySenderId() != null) {
												if (collaboration.getIUserBySenderId().getStatus() == IUser.Status.REGISTERED
														&& !collaboration.getIUserBySenderId().equals(me)
														&& collaboration.getSenderVisibility().equals(
																ICollaboration.Visibility.VISIBLE)) {
													ownerFound = true;
								%>
								<span class="medium-text">Request by <a
									href="<%=SwimConst.Url.Servlet.PROFILE%>?who=
									<%=collaboration.getIUserBySenderId().getId()%>"><%=SwimUtils.getName(collaboration
									.getIUserBySenderId())%></a></span><br />
								<%
									} else if (collaboration.getIUserBySenderId().equals(me)) {
													ownerFound = true;
								%>
								<span class="medium-text">Your request to <a
									href="<%=SwimConst.Url.Servlet.PROFILE%>?a=<%=Action.VIEWPROFILE%>&who=<%=collaboration.getUserByReceiverId().getId()%>"><%=SwimUtils.getName(collaboration
									.getUserByReceiverId())%></a></span><br />
								<%
									} else if (collaboration.getIUserBySenderId().getStatus() == IUser.Status.UNREGISTERED) {
													ownerFound = true;
								%>
								<span class="medium-text"><a
									href="mailto:<%=collaboration.getIUserBySenderId().getEmail()%>"><%=collaboration.getIUserBySenderId().getEmail()%></a></span><br />
								<%
									}
											}
										}
								%>


								<%
									if (!ownerFound) {
								%>
								<span class="medium-text">Anonymous request</span><br />
								<%
									}
								%>

								<small>Last message <%=SwimUtils.formatDate(lastmessage.getTimestamp(),
						"dd/MM/yyyy")%>, <%=SwimUtils.formatDate(lastmessage.getTimestamp(),
						"HH:mm")%></small><br />
							</h4>
							<p class=""><%=lastmessage.getMessage()%></p>
							<p>
								<a class="btn btn-primary"
									href="<%=SwimConst.Url.Servlet.COLLABORATION%>?a=<%=Action.VIEWCOLLABORATION%>&id=<%=collaboration.getId()%>">View</a>
								<%
									if (collaboration.getUserByReceiverId().equals(me)
												&& collaboration.getStatus() == Status.REQUESTED) {
								%>
								<span class="btn-group"> <a class="btn btn-success"
									href="<%=SwimConst.Url.Servlet.COLLABORATION%>?a=<%=Action.ACCEPTCOLLABORATION%>&collaborationId=<%=collaboration.getId()%>">Accept</a>
									<a class="btn btn-danger"
									href="<%=SwimConst.Url.Servlet.COLLABORATION%>?a=<%=Action.REJECTCOLLABORATION%>&collaborationId=<%=collaboration.getId()%>">Reject</a>
								</span>
								<%
									}
								%>
							</p>
						</div>
					</div>
					<%
						}
					%>
					<%
						List otherColls = (List) request.getAttribute("otherColls");
						for (int i = 0; i < otherColls.size(); i++) {
							collaboration = (ICollaboration) otherColls.get(i);
							lastmessage = ((ICollaboration) otherColls.get(i))
									.getMessagesList().get(0);
							
							if(i%2 == 0){
								%>
								<div class="row" style="padding-left: 15px">
								<%
							}
					%>
					<div class="span4">
						<div class="collaboration-item small-text ui-corner-all">
							<%
								if (collaboration.getStatus().equals(Status.COMPLETED)) {
							%>
							<div class="label label-info pull-right">Completed</div>
							<%
								} else if (collaboration.getStatus().equals(Status.CONFIRMED)) {
							%>
							<div class="label label-success pull-right">Accepted</div>
							<%
								} else if (collaboration.getStatus().equals(Status.REJECTED)) {
							%>
							<div class="label label-important pull-right">Rejected</div>
							<%
								} else if (collaboration.getStatus().equals(Status.REQUESTED)) {
							%>
							<div class="label label-warning pull-right">Requested</div>
							<%
								}
							%>
							<h5>
								<%=collaboration.getTitle()%><br />
								<%
									boolean ownerFound = false;
										if (SwimUtils.isLogged(request)) {

											if (collaboration.getIUserBySenderId() != null) {
												if (collaboration.getIUserBySenderId().getStatus() == IUser.Status.REGISTERED
														&& !collaboration.getIUserBySenderId().equals(
																me)
														&& collaboration.getSenderVisibility().equals(
																ICollaboration.Visibility.VISIBLE)) {
													ownerFound = true;
								%>
								<span class="small-text">Request by <a
									href="<%=SwimConst.Url.Servlet.PROFILE%>?who=
									<%=collaboration.getIUserBySenderId()
									.getId()%>"><%=SwimUtils.getName(collaboration
									.getIUserBySenderId())%></a></span><br />
								<%
									} else if (collaboration.getIUserBySenderId()
														.equals(me)) {
													ownerFound = true;
								%>
								<span class="small-text">Your request to <a
									href="<%=SwimConst.Url.Servlet.PROFILE%>?a=<%=Action.VIEWPROFILE%>&who=<%=collaboration.getUserByReceiverId()
									.getId()%>"><%=SwimUtils.getName(collaboration
									.getUserByReceiverId())%></a></span><br />
								<%
									} else if (collaboration.getIUserBySenderId()
														.getStatus() == IUser.Status.UNREGISTERED) {
													ownerFound = true;
								%>
								<span class="small-text"><a
									href="mailto:<%=collaboration.getIUserBySenderId()
									.getEmail()%>"><%=collaboration.getIUserBySenderId()
									.getEmail()%></a></span><br />
								<%
									}
											}
										}
								%>


								<%
									if (!ownerFound) {
								%>
								<span class="small-text">Anonymous request</span><br />
								<%
									}
								%>

								<small class="small">Last message <%=SwimUtils.formatDate(lastmessage.getTimestamp(),
						"dd/MM/yyyy")%></small>
							</h5>
							<p class=""><%=lastmessage.getMessage()%></p>
							<div>
								<a class="btn btn-primary btn-small"
									href="<%=SwimConst.Url.Servlet.COLLABORATION%>?a=<%=Action.VIEWCOLLABORATION%>&id=<%=collaboration.getId()%>">View</a>
								<%
									if (collaboration.getUserByReceiverId().equals(me)
												&& collaboration.getStatus() == Status.REQUESTED) {
								%>
								<span class="btn-group"> <a
									class="btn btn-small btn-success"
									href="<%=SwimConst.Url.Servlet.COLLABORATION%>?a=<%=Action.ACCEPTCOLLABORATION%>&collaborationId=<%=collaboration.getId()%>">Accept</a>
									<a class="btn btn-small btn-danger"
									href="<%=SwimConst.Url.Servlet.COLLABORATION%>?a=<%=Action.REJECTCOLLABORATION%>&collaborationId=<%=collaboration.getId()%>">Reject</a>
								</span>
								
								<%
									}
								%>
							</div>
						</div>
					</div>
					<%if(i%2 == 1 || i==(otherColls.size() - 1)){
						%>
						</div>
						<%
					}
						}
					%>
				</div>
			</div>
			<%@include file="include/notifications.jsp"%>
		</div>
	</div>
	<%@include file="include/footer.jsp"%>
	<%@include file="include/script-common.jsp"%>
</body>
</html>